
function message(code, message, data) {

    layui.use('layer', function () {
        var layer = layui.layer;
        if (code === 200) {

        }
        if (code !== 200) {
            layer.msg(message);
        }
    })

}

function checkResult(code, message, data, callback) {
    if (code === 200) {
        callback();
    }
    if (code === 400) {
        layer.msg(JSON.stringify(data));
    } else {
        layer.msg(message);
    }
}

layui.use([ 'layer', 'setter', 'request', 'user', 'view', 'helper', 'jquery' ], function () {

    var events = layui.setter.events
        , layer = layui.layer
        , helper = layui.helper
        , $ = layui.jquery
        , request = layui.request;

    events.create = function (option) {

        var config = option.config
            , tableIns = option.tableIns
            , data = option.data;

        config.before && config.before(data);

        layer.open({
            type: config.type ?? 1,
            shade: config.shade ?? true, // 不显示遮罩
            title: config.title,
            content: $(config.targetId), // 捕获的元素
            area: config.area,
            end: function () {
                tableIns.reload();
                config.after && config.after();
            }
        });

    };

    events.update = function (option) {

        var config = option.config
            , tableIns = option.tableIns
            , data = option.data;

        config.before && config.before(data);

        layer.open({
            type: config.type ?? 1,
            shade: config.shade ?? true, // 不显示遮罩
            title: config.title,
            content: $(config.targetId), // 捕获的元素
            area: config.area,
            end: function () {
                tableIns.reload();
                config.after && config.after();
            }
        });

    };

    events.delete = function (option) {
        var {before, title, url, formType, value, maxlength, sendData, method, message} = option.config
            , tableIns = option.tableIns
            , data = option.data;

        before && before();

        if (!url) {
            layer.alert('请先配置delete请求地址');
            return false
        }

        layer.alert(message??"确定删除该项目？", function (index) {
            var requestData = sendData(value, option.data);
            request.send(url, "POST", requestData, true, function (code, msg, res) {
                if (code === 200) {
                    tableIns.reload();
                    layer.close(index);
                } else {
                    layer.msg(msg);
                }
            })
        })
    }

    events['reset-password'] = function (option) {

        var {before, title, url, formType, value, maxlength, sendData, method} = option.config
            , tableIns = option.tableIns
            , data = option.data;

        before && before();

        layer.prompt({
            formType: formType ?? 0, //输入框类型，支持0（文本）默认1（密码）2（多行文本）
            value: value ?? '', //初始时的值，默认空字符
            maxlength: maxlength ?? 140, //可输入文本的最大长度，默认500
            title: helper.templateReplace(title, data) ?? '修改密码'
        }, function (value, index, elem) {

            if (!url) {
                layer.alert('请先配置请求地址');
                return false
            }

            var requestData = sendData(value, option.data);
            request.send(url, "POST", requestData, true, function (code, msg, res) {
                if (code === 200) {
                    layer.close(index);
                } else {
                    layer.msg(msg);
                }
            })
        });

    };

    events.setting = function (option) {
        var {company} = option.config;
        var content = "<table class=\"layui-table\" lay-skin=\"nob\"> " +
            "<tr><td style='text-align: center'>版本：YakAdmin V1.0 <i>MIT license</i></td></tr>" +
            "<tr><td style='text-align: center'>CopyRight © 2020 " + company + " All Rights Reserved.</td></tr>" +
            "</table>"
        layer.open({
            type: 1,
            title: "<i class=\"layui-icon\">&#xe60b;</i> 关于",
            area: ['500px', '150px'],
            maxmin: false,
            zIndex: 9999999,
            shade: 0.8,
            content: content
        })
    };

    events.logout = function (option) {
        layer.confirm('是否注销系统？', {
            btn: ['确定', '关闭']
        }, function () {
            layui.user.logout();
            window.location.reload();
        }, function () {
        });
    };


})